import router from "./router";
import nProgress from "nprogress";
import 'nprogress/nprogress.css'
import store from "./store";

nProgress.configure({ showSpinner: false })
const wihteList = ['/login', '/404']
// 前置守卫
router.beforeEach(async (to, from, next) => {
  nProgress.start()
  if (store.getters.token) {
    // 有token
    if (to.path === '/login') {
      next('/')
      nProgress.done()
    } else {
      if (!store.getters.userId) {
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
  } else {
    // 没有token
    if (wihteList.indexOf(to.path) > -1) {
      next()
    } else {
      next('/login')
      nProgress.done()
    }
  }
})

// 后置守卫
router.afterEach((to, from, next) => {
  nProgress.done()
})
